<!-- 新增合伙人 -->
<template>
  <el-drawer
    title="新增合伙人"
    :before-close="handleClose"
    :visible.sync="addSonCardDialog"
    size="40%"
    ref="drawerAddSonCard"
  >
    <div style="margin: 0px 5%">
      <el-form
        :model="formAddSonCard"
        :rules="rulesAddSonCard"
        ref="refAddSonCard"
        size="small"
        label-position="top"
      >
        <div class="test-form">基础信息</div>

        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item
              label="合伙人名称"
              :label-width="formLabelWidth"
              style="margin-bottom: 30px"
              prop="nickName"
            >
              <el-input
                :disabled="partnersIs === 'view'"
                v-model="formAddSonCard.nickName"
                placeholder="请输入合伙人名称"
                maxlength="25"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="联系人"
              :label-width="formLabelWidth"
              style="margin-bottom: 30px"
              prop="partnerName"
            >
              <el-input
                :disabled="partnersIs === 'view'"
                v-model="formAddSonCard.partnerName"
                placeholder="请输入联系人"
                maxlength="11"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              label="联系电话"
              :label-width="formLabelWidth"
              style="margin-bottom: 30px"
              prop="phonenumber"
              maxlength="11"
            >
              <el-input
                :disabled="partnersIs === 'view'"
                v-model="formAddSonCard.phonenumber"
                placeholder="请输入联系电话"
                maxlength="11"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <div class="test-form">状态设置</div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              label="合伙人状态"
              :label-width="formLabelWidth"
              prop="status"
            >
              <el-select
                style="width: 100%"
                v-model="formAddSonCard.status"
                :clearable="true"
                filterable
                placeholder="请选择合伙人状态"
                :disabled="partnersIs === 'view'"
              >
                <el-option label="启用" value="0"></el-option>
                <el-option label="停用" value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="demo-drawer-footer">
        <el-button
          @click="submitForm"
          type="primary"
          size="small"
          v-show="partnersIs !== 'view'"
          >确 定</el-button
        >

        <el-button @click="handleClose" size="small">取 消</el-button>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import { systemUser, editPartner } from "@/api/dept/partnersList";
export default {
  name: "PartnersListAdd",
  props: {
    addSonCardDialog: {
      type: Boolean,
      default: false,
    },
    partnersObj: {
      type: Object,
      default: () => {},
    },
    partnersIs: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      formAddSonCard: {
        nickName: null,
        partnerName: null,
        phonenumber: null,
        status: null,
      },
      formLabelWidth: "80px",

      rulesAddSonCard: {
        nickName: [
          {
            required: true,
            message: "合伙人名称",
            trigger: ["change", "blur"],
          },
        ],
        partnerName: [
          {
            required: true,
            message: "请输入联系人",
            trigger: ["change", "blur"],
          },
        ],
        phonenumber: [
          {
            required: true,
            message: "请输入联系电话",
            trigger: ["change", "blur"],
          },
        ],
        status: [
          {
            required: true,
            message: "请选择合伙人状态",
            trigger: ["change", "blur"],
          },
        ],
      },
    };
  },

  methods: {
    submitForm() {
      this.$refs.refAddSonCard.validate((valid) => {
        if (valid) {
          const formAddSonCard = {
            nickName: this.formAddSonCard.nickName,
            partnerName: this.formAddSonCard.partnerName,
            phonenumber: this.formAddSonCard.phonenumber,
            status: this.formAddSonCard.status,
            userName: this.formAddSonCard.phonenumber,
          };

          if (this.partnersIs == "add") {
            systemUser(formAddSonCard).then((res) => {
              if (res && res.code == 200) {
                this.$message.success("新增合伙人成功");
                this.handleClose();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            editPartner(formAddSonCard).then((res) => {
              if (res && res.code == 200) {
                this.$message.success("修改合伙人成功");
                this.handleClose();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          console.log("error submit!!");
        }
      });
    },
    handleClose() {
      this.$refs.refAddSonCard.resetFields();
      this.$emit("update:addSonCardDialog", false);
    },
  },
  watch: {
    addSonCardDialog: {
      deep: true, // 深度监听
      handler(newVal, oldVal) {
        if (this.partnersIs == "add") {
          this.formAddSonCard = {
            nickName: null,
            partnerName: null,
            phonenumber: null,
            status: null,
          };
        } else {
          this.formAddSonCard = this.partnersObj;
        }
      },
    },
  },
};
</script>
<style scoped lang="scss">
.test-form {
  width: 100%;
  font-size: 18px;
  margin: 30px 0px 20px 0px;
  border-bottom: 1px solid #ccc;
}
.demo-drawer-footer {
  width: 100%;
  position: absolute;
  right: -75%;
  bottom: 15px;
}
</style>
